/*
 * ConnectionManager.hpp
 *
 *  Created on: Nov 21, 2014
 *      Author: toti
 */

#ifndef CONNECTIONMANAGER_HPP_
#define CONNECTIONMANAGER_HPP_

#include <sys/epoll.h>

#include <list>

#include <Misc/Config.hpp>
#include <Connectivity/EPoll.hpp>
#include <Connectivity/TCPServer.hpp>
#include <Connectivity/TCPClient.hpp>
#include <Connectivity/TCPConnection.hpp>
#include <Network/LocalServer.hpp>
#include <Network/LocalClient.hpp>
#include <Network/MessagePackages/MessagePackage.hpp>
#include <Network/MessagePackages/ServerMessagePackage.hpp>
#include <Network/MessagePackages/ClientMessagePackage.hpp>
#include <Network/NetworkServer.hpp>
#include <Network/NetworkClient.hpp>

class ConnectionManager{

private:


	/*
	 * Message lists
	 */
	std::list<MessagePackage*> *inboundMessagePackageList;
	std::list<MessagePackage*> *outboundMessagePackageList;

	/*
	 * Local nodes
	 */
	LocalServer *parentServer;
	std::list<LocalNode*> *localServerList;
	std::list<LocalNode*> *localClientList;

	/*
	 * Listen server
	 */
	TCPServer *localClientTcpServer;
	TCPServer *localServerTcpServer;

	/*
	 * epoll stuff
	 */
	EPoll *epoll;

	void handleExistingLocalNodeError(LocalNode *localNode);

	void handleNewLocalClientConnections(TCPServer *tcpServer);
	void handleNewLocalServerConnections(TCPServer *tcpServer);

	void handleExistingLocalNodeConnection(LocalNode *localNode);


public:

	ConnectionManager();

	void initParentServer();
	void initChildServer();

	void stopServerListening();

	void handleInputData();
	void handleOutputData();

	LocalNode *getLocalParentServer();
	std::list<LocalNode*> *getLocalClientList();
	std::list<LocalNode*> *getLocalServerList();

	void pushOutboundMessagePackageList(MessagePackage *messagePackage);
	MessagePackage *popInboundMessagePackageList();

};


#endif /* CONNECTIONMANAGER_HPP_ */
